/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAMagazineTableViewController.m
//  redia-bapps-ios
//
//  Created by Brian Vestergaard Danielsen on 04/11/13.
//  Copyright (c) 2013 Redia A/S. All rights reserved.
//

#import "BAMagazineTableViewController.h"
#import "BAMagazineVolumeCell.h"
#import "BAMaterialViewController.h"
#import "BAMagazineCell.h"
#import "BARootViewController.h"

@interface BAMagazineTableViewController ()

@property (weak, nonatomic) IBOutlet UITableView *materialsTableView;
@property (weak, nonatomic) IBOutlet UILabel *subPublicationTitle;

@property (strong, nonatomic) BAAvailability* availability;
@property (strong, nonatomic) BAFullRecord* fullRecord;
@property (strong, nonatomic) NSString* volumeTitle;

@end

@implementation BAMagazineTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    BA_LABEL_SET_FONT_LIGHT(self.subPublicationTitle);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAvailability:(BAAvailability*)availability andFullRecord:(BAFullRecord*)fullRecord withVolumeTitle:(NSString*)volumeTitle andShortVolumeTitle:(NSString *)shortVolumeTitle;
{
    _availability = availability;
    _fullRecord = fullRecord;
    _volumeTitle = volumeTitle;
    if (RD_IS_IPAD) {
        self.subPublicationTitle.text = shortVolumeTitle;
    } else  {
        self.subPublicationTitle.text = volumeTitle;
    }
    [self.materialsTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [[[NSBundle mainBundle] loadNibNamed:@"BAMagazineVolumeCell" owner:self options:nil] objectAtIndex:0];
    BAHoldingContainer* holdingContainer = [self.availability.holdingContainers objectAtIndex:indexPath.row];
    if([holdingContainer.availability.holdingContainers count] == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"BAMagazineCell"];
        if(cell == nil){
            cell = [[[NSBundle mainBundle] loadNibNamed:@"BAMagazineCell" owner:self options:nil] objectAtIndex:0];
        }
        [(BAMagazineCell*)cell setHoldingContainer:holdingContainer withVolumeTitle:self.volumeTitle];
    } else {
        [(BAMagazineVolumeCell*)cell setVolume:[self.availability.holdingContainers objectAtIndex:indexPath.row]];
    }

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.availability.holdingContainers count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BAHoldingContainer* container = [self.availability.holdingContainers objectAtIndex:indexPath.row];
    if([container.availability.holdingContainers count] == 0) {
        return 50;
    } else {
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BAHoldingContainer* clickedContainer = [self.availability.holdingContainers objectAtIndex:indexPath.row];
    BAAvailability* availability = clickedContainer.availability;
    if([availability.holdingContainers count] != 0) {
        UIViewController<BAMaterialViewControllerProtocol>* mvc = [[BARootViewController instance] createMaterialViewController];
        [self.navigationController pushViewController:mvc animated:YES];
        
        NSString* newVolumeTitle = [NSString stringWithFormat:@"%@, %@", self.volumeTitle, clickedContainer.title];
        
        [mvc showMagazineFullRecord:self.fullRecord andAvailability:availability withVolumeTitle:newVolumeTitle andShortVolumeTitle:clickedContainer.title];
    } else {
        
    }
}

@end